package life.mahu.community.controller;

import life.mahu.community.dto.PaginationDTO;
import life.mahu.community.model.Category;
import life.mahu.community.model.User;
import life.mahu.community.service.CategoryService;
import life.mahu.community.service.NotificationService;
import life.mahu.community.service.QuestionService;
import life.mahu.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ProfileController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private NotificationService notificationService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Value("${github.client.id}")
    private String githubClientId;

    @Value("${mayun.client.id}")
    private String mayunClientId;

    @GetMapping(value = {"/profile/{action}/{userId}", "/profile/{action}"})
    public String profile(HttpServletRequest request,
                          @PathVariable(name = "action") String action,
                          @PathVariable(name = "userId", required = false) Long userId,
                          Model model,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "5") Integer size) {

        // 查询分类
        List<Category> categoryList = categoryService.getList();
        User user;
        if (userId == null) {
            user = (User) request.getSession().getAttribute("user");
            if (user == null) {
                return "redirect:/";
            }
        } else
            user = userService.getUserById(userId);

        if ("questions".equals(action)) {
            model.addAttribute("userInfo", user);
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的提问");
            PaginationDTO paginationDTO = questionService.list(user.getId(), page, size, 0);
            model.addAttribute("pagination", paginationDTO);
        } else if ("like".equals(action)) {
            model.addAttribute("userInfo", user);
            model.addAttribute("section", "like");
            model.addAttribute("sectionName", "我的收藏");
            PaginationDTO paginationDTO = questionService.list(user.getId(), page, size, 1);
            model.addAttribute("pagination", paginationDTO);
        } else if ("replies".equals(action)) {
            PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
            model.addAttribute("userInfo", user);
            model.addAttribute("section", "replies");
            model.addAttribute("pagination", paginationDTO);
            model.addAttribute("sectionName", "最新回复");
        } else if ("account".equals(action)) {
            model.addAttribute("userInfo", user);
            model.addAttribute("section", "account");
            model.addAttribute("sectionName", "个人信息");
        } else if ("updatePassword".equals(action)) {
            model.addAttribute("userInfo", user);
            model.addAttribute("section", "updatePassword");
            model.addAttribute("sectionName", "修改密码");
        } else if ("escrowAccount".equals(action)) {
            model.addAttribute("githubClientId", githubClientId);
            model.addAttribute("mayunClientId", mayunClientId);
            model.addAttribute("userInfo", user);
            model.addAttribute("escrowAccount", userService.getEscrowAccount(user));
            model.addAttribute("section", "escrowAccount");
            model.addAttribute("sectionName", "第三方账户");
        }
        model.addAttribute("categorys", categoryList);
        return "profile";
    }
}
